package src.greed;

import java.util.Arrays;

public class no435 {//无重叠区间
    public static void main(String[] args) {
        int[][] arr = {{1,1,1},{2,2,6},{3,3,5}};
        Arrays.sort(arr,(a,b)->a[2]-b[2]);
        System.out.println(Arrays.deepToString(arr));
    }

    public int eraseOverlapIntervals(int[][] intervals) {
        //按照第0位给二维数组排序, lamda表达式写法
        Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
        int count = 0;
        for (int i = 1; i < intervals.length; i++) {
            //当前 i 的左边界 比 i-1 的右边界还小，就计数，这个区间要删除
            if (intervals[i][0] < intervals[i - 1][1]) {
                count++;
                //将当前 i 的右边界更新为 i和i-1 中更小的右边界
                intervals[i][1] = Math.min(intervals[i][1], intervals[i - 1][1]);
            }
        }
        return count;
    }

}
